package algorithm.leetcode.simple;

/*
 给定一个字符串，验证它是否是回文串，只考虑字母和数字字符，可以忽略字母的大小写。
 */

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/10/14
 */
public class Question125 {

    public static void main(String[] args) {
        String str = "race a car";
        Question125 question = new Question125();
        boolean isPalindrome = question.isPalindrome(str);
        System.out.println(isPalindrome);
    }

    public boolean isPalindrome(String s) {
        String str = s.toLowerCase();
        boolean isPalindrome = true;
        int left = 0;
        int right = str.length() - 1;
        while (left < right) {
            char leftCh = str.charAt(left);
            char rightCh = str.charAt(right);
            if (!isNumAndChar(leftCh)) {
                left++;
                continue;
            }
            if (!isNumAndChar(rightCh)) {
                right--;
                continue;
            }
            if (leftCh != rightCh) {
                isPalindrome = false;
                break;
            }
            left++;
            right--;
        }
        return isPalindrome;
    }

    private boolean isNumAndChar(char ch) {
        return (ch >= 48 && ch <= 57) || (ch >= 97 && ch <= 122);
    }
}
